//
//  FingerPassViewController.m
//  Monosaccharides
//
//  Created by iSolar on 2017/11/15.
//  Copyright © 2017年 nothing. All rights reserved.
//

#import "FingerPassViewController.h"
#import <ColorExtension/UIColor+Hex.h>
#import <LocalAuthentication/LocalAuthentication.h>
#import <SVProgressHUD/SVProgressHUD.h>

@interface FingerPassViewController ()

@end

@implementation FingerPassViewController

/*
 //授权失败
 LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
 //用户取消Touch ID授权
 LAErrorUserCancel           = kLAErrorUserCancel,
 
 //用户选择输入密码
 LAErrorUserFallback         = kLAErrorUserFallback,
 
 //系统取消授权(例如其他APP切入)
 LAErrorSystemCancel         = kLAErrorSystemCancel,
 
 //系统未设置密码
 LAErrorPasscodeNotSet       = kLAErrorPasscodeNotSet,
 
 //设备Touch ID不可用，例如未打开
 
 LAErrorTouchIDNotAvailable  = kLAErrorTouchIDNotAvailable,
 
 //设备Touch ID不可用，用户未录入
 LAErrorTouchIDNotEnrolled   = kLAErrorTouchIDNotEnrolled,
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"oc指纹解锁";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *fingerPassButton = [[UIButton alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 100)/2, 100, 100, 35)];
    [fingerPassButton setTitle:@"点我解锁吧" forState:UIControlStateNormal];
    [fingerPassButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    fingerPassButton.layer.cornerRadius = 5;
    fingerPassButton.backgroundColor = [UIColor colorWithHexString:@"12adc3"];
    [fingerPassButton addTarget:self action:@selector(fingerPassAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:fingerPassButton];
    
}

- (void)fingerPassAction {
    
    // 初始化上下文
    LAContext *context = [[LAContext alloc] init];
    NSError *error = nil;
    NSString *msg = @"使用你的设备指纹解锁";
    
    //判断设备状态
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        // 指纹验证
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:msg reply:^(BOOL success, NSError * _Nullable error) {
            
            if (success) {
                //验证成功，直接在主线程刷新UI
                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    [SVProgressHUD showSuccessWithStatus:@"解锁成功"];
                }];
            } else {
                
                NSLog(@"%@", error.localizedDescription);
                
                switch (error.code) {
                    case LAErrorSystemCancel:
                        //切换到其他APP 系统取消验证
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            [SVProgressHUD showErrorWithStatus:@"系统取消验证"];
                        }];
                    }
                        break;
                      
                    case LAErrorUserCancel:
                        //用户取消验证
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            [SVProgressHUD showErrorWithStatus:@"用户取消验证"];
                        }];
                    }
                        break;
                    case LAErrorUserFallback:
                        //用户验证失败
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            [SVProgressHUD showErrorWithStatus:@"用户验证失败"];
                        }];
                    }
                        break;
                    default:
                        //其他失败情况
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            [SVProgressHUD showErrorWithStatus:@"其他失败情况"];
                        }];
                    }
                        break;
                }
                
            }
            
        }];
    } else {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [SVProgressHUD showErrorWithStatus:@"您的设备不支持指纹解锁"];
        }];
    }
    
}

@end
